package com.laicunba.service.jzh.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;

import com.laicunba.service.jzh.JzhRegService;
import com.laicunba.service.jzh.SecurityUtils;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.HttpUtil;

@Service
public class JzhRegServiceImpl extends JzhBaseService implements JzhRegService {
	private static final Logger LOG = Logger.getLogger(JzhRegServiceImpl.class);
	private static final char SPLIT_CHAT = '|';

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Result post(Param param) {

		param.setMchntCd(this.mchntCd);

		StringBuilder signSrc = new StringBuilder();
		signSrc.append(param.getBankNm()).append(SPLIT_CHAT);
		signSrc.append(param.getCapAcntNm()).append(SPLIT_CHAT);
		signSrc.append(param.getCapAcntNo()).append(SPLIT_CHAT);
		signSrc.append(param.getCertifId()).append(SPLIT_CHAT);
		signSrc.append(param.getCityId()).append(SPLIT_CHAT);
		signSrc.append(param.getCustNm()).append(SPLIT_CHAT);
		signSrc.append(param.getEmail()).append(SPLIT_CHAT);
		signSrc.append(param.getLpassword()).append(SPLIT_CHAT);
		signSrc.append(param.getMchntCd()).append(SPLIT_CHAT);
		signSrc.append(param.getMchntTxnSsn()).append(SPLIT_CHAT);
		signSrc.append(param.getMobileNo()).append(SPLIT_CHAT);
		signSrc.append(param.getParentBankId()).append(SPLIT_CHAT);
		signSrc.append(param.getPassword()).append(SPLIT_CHAT);
		signSrc.append(param.getRem()).append(SPLIT_CHAT);
		signSrc.append(param.getVer());
		LOG.info("sign source string : " + signSrc.toString()); 
		String signature = SecurityUtils.sign(signSrc.toString());
		LOG.info("signature is : " + signature);

		param.setSignature(signature);
		Map map = new HashMap();
		map.put("ver", param.getVer());
		map.put("mchnt_cd", param.getMchntCd());
		map.put("mchnt_txn_ssn", param.getMchntTxnSsn());
		map.put("cust_nm", param.getCustNm());
		map.put("certif_tp", param.getCertifTp());
		map.put("certif_id", param.getCertifId());
		map.put("mobile_no", param.getMobileNo());
		map.put("email", param.getEmail());
		map.put("city_id", param.getCityId());
		map.put("parent_bank_id", param.getParentBankId());
		map.put("bank_nm", param.getBankNm());
		map.put("capAcntNm", param.getCapAcntNm());
		map.put("capAcntNo", param.getCapAcntNo());
		map.put("password", param.getPassword());
		map.put("lpassword", param.getLpassword());
		map.put("rem", param.getRem());
		map.put("signature", URLEncoder.encode(param.getSignature()));
		try {
			String xmlResult = HttpUtil.doPost(this.mchntURL + "/reg.action", map);
			Document doc = parseXml(xmlResult.getBytes());

			Result result = new Result();
			result.setRespCode(getValue(doc, "resp_code"));
			result.setMchntCd(getValue(doc, "mchnt_cd"));
			result.setRespDesc(getValue(doc, "resp_desc"));
			result.setSignature(getValue(doc, "signature"));
			result.setMchntTxnSsn(getValue(doc, "mchnt_txn_ssn"));
			LOG.info("result: " + result);
			return result;
		} catch (Exception e) {
			AssertUtil.assertFail("ERROR");
			LOG.error("", e);
		}
		return null;
	}

}
